package com.fy56.platform.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.ServerManage;
import com.fy56.platform.logistics.service.web.IServerManageService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 服务器信息controller
 * Created by li on 2018-02-09.
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/controller/server")
public class ServerController extends BaseController {

    @Autowired
    private IServerManageService iServerManageService;

    /**
     * 获取服务器信息
     *
     * @param keyId
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public ServerManage getServerManage(Long keyId, HttpServletRequest request){
        ServerManage server = new ServerManage();
        if(keyId != null){
            server = iServerManageService.selectByPrimaryKey(keyId);
        }
        return server;
    }

    /**
     * 添加保存
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(ServerManage server, HttpServletRequest request) {
        ResultModel resultModel = null;
        List<ServerManage> searchList = null;
        try {
            if (server != null) {

                //验证服务器是否存在，若服务器已存在，提示：该服务器已经存在,请重新输入
                searchList = iServerManageService.select(server);
                if (searchList != null && searchList.size() > 0) {
                    return new ResponseEntity<ResultModel>(new ResultModel("该服务器已经存在,请重新输入!"), HttpStatus.OK);
                }

                //执行插入
                server.initInsert(SessionUtil.getSessionUser(request));
                resultModel = iServerManageService.insert(server);

                //保存系统业务日志
                LogUtils.saveLog(request, resultModel.getMsg(), server.getId().toString(), null, "ServerManage", "save", "添加", JSON.toJSONString(server));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(ResultStatus.INSERT_ERR), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            String key = (server == null || server.getId() == null) ? null : server.getId().toString();
            LogUtils.saveLog(request, "失败", key, null, "ServerManage", "save", "添加", JSON.toJSONString(server));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            searchList = null;
        }
    }

    /**
     * 修改保存
     *
     * @param server
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> update(ServerManage server, HttpServletRequest request) {
        ResultModel resultModel = null;
        ServerManage dbModel = null;
        try{
            if (server != null) {

                //根据主键查询DB中的记录
                dbModel = iServerManageService.selectByPrimaryKey(server.getId());

                //如果DB中的外网IP与页面不等时，提示：外网IP不允许修改！
                if(!dbModel.getOuterIp().equals(server.getOuterIp())){
                    return new ResponseEntity<ResultModel>(new ResultModel("外网IP不允许修改!"), HttpStatus.OK);
                }else{

                    //设置修改操作员，修改时间（默认当前时间）
                    server.initUpdate(SessionUtil.getSessionUser(request));

                    //执行更新操作
                    resultModel = iServerManageService.update(server);

                    //保存系统业务日志
                    LogUtils.saveLog(request,"成功",server.getId().toString(),null,"ServerManage","update","编辑", JSON.toJSONString(server));
                }
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(ResultStatus.EDIT_ERR), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",server.getId().toString(),null,"ServerManage","update","编辑", JSON.toJSONString(server));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            dbModel = null;
        }
    }

    /**
     * 删除
     *
     * @param keyId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> delete(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        ServerManage server = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                server = new ServerManage();
                server.setId(keyId);
                server.setDeleteFlag(1);
                server.initUpdate(SessionUtil.getSessionUser(request));

                //执行删除操作
                resultModel = iServerManageService.delete(server);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"ServerManage","delete","删除", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("服务器删除失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"ServerManage","delete","删除", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            server = null;
        }
    }

    /**
     * 批量删除
     *
     * @param keyIds
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> batchDelete(String keyIds, HttpServletRequest request) {
        ResultModel resultModel = null;
        LinkedList<ServerManage> list = null;
        try{
            if (StringUtil.isNotEmpty(keyIds)) {

                //构建删除对象（服务器对象的集合）
                list = new LinkedList<ServerManage>();
                ServerManage server = null;
                for(String id : keyIds.split(",")){
                    if(StringUtil.isNotEmpty(id)){
                        server = new ServerManage();
                        server.setDeleteFlag(1);
                        server.initUpdate(SessionUtil.getSessionUser(request));
                        server.setId(Long.parseLong(id));
                        list.add(server);
                    }
                }

                //执行删除操作
                resultModel = iServerManageService.batchDelete(new ArrayList<ServerManage>(list));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("服务器信息批量删除失败!"), HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {

            //保存系统业务日志
            LogUtils.saveLog(request,resultModel.getMsg(),keyIds,null,"ServerManage","batchDelete","批量删除",keyIds);
            resultModel = null;
            list = null;
        }
    }

}
